package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import model.User;
import util.SocketUtil;
import util.StateCode;

public class InfoPanel extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultTableModel dtm;
	private JLabel player;
	private JTextArea chat;
	public InfoPanel() {
		setBackground(Color.yellow);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		JPanel top=new JPanel();
		top.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		JLabel label1=new JLabel(new ImageIcon(getClass().getResource("../images/p1.jpg")));
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets=new Insets(20, 0, 10, 0);
		top.add(label1, gbc);
		player=new JLabel("玩家x");
		gbc.gridy=1;
		top.add(player, gbc);
		add(top);
		JPanel middle=new JPanel();
		middle.setLayout(new BorderLayout());
		dtm=new DefaultTableModel(new Object[][]{},new String[]{"昵称","IP","联机时间"});
		JTable table=new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane jsp=new JScrollPane(table);
		jsp.setPreferredSize(new Dimension(230,200));
		middle.add(jsp);
		add(middle);
		JPanel bottom=new JPanel();
		bottom.setLayout(new BorderLayout());
		chat=new JTextArea();
		chat.setPreferredSize(new Dimension(200, 500));
		JPanel panel=new JPanel();
		panel.setLayout(new BorderLayout());
		JTextField jtf=new JTextField();
		JButton send=new JButton("发送");
		send.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String message=SocketUtil.getUser().getName()+":"+jtf.getText()+"\n";
				chat.append(message);
				SocketUtil.SendMessage(message);
				jtf.setText("");
			}
		});
		panel.add(jtf);
		panel.add(send,BorderLayout.EAST);
		bottom.add(chat);
		bottom.add(panel,BorderLayout.SOUTH);
		add(bottom);
	}
	
	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof User)//如果改变类型为User
		{
			User user=(User) arg;
			dtm.addRow(new Object[]{user.getName(),user.getIp(),user.getLogintime()});
			if(o.equals(user))//如果观察者包含user
				player.setText(user.getName());
		}
			else if(arg instanceof String)
			{
				 String message=(String) arg;
				chat.append(message);
			}
			else if(arg instanceof Integer)//如果改变类型为整形
			{
				switch((int) arg){
				case StateCode.LINKREST://设置连接重置
					for(int i=0;i<dtm.getRowCount();i++)
						dtm.removeRow(i);
						break;
				}
			}
	}

}
